package main

import (
	"communication-system/server/model"
	"fmt"
	"net"
	"time"
)

//deal with the comunication with client
func process(conn net.Conn) {
	defer conn.Close()
	//read the mes from clients (loop)

	processor := &Processor{
		Conn: conn,
	}
	err := processor.process2()
	if err != nil {
		fmt.Println("the grounine of client and server is err=", err)
		return
	}

}

func initUserDao() {
	model.MyUserDao = model.NewUserDao(pool)
}

func main() {
	initPool("localhost:6379", 16, 0, 300 * time.Second)
	initUserDao()
	fmt.Println("server(new) listen the port 8889...")
	listen, err := net.Listen("tcp", "127.0.0.1:8889")
	defer listen.Close()
	if err != nil {
		fmt.Println("net.Listen err=", err)
		return
	}
	//listen success,wait client to connect
	for {
		fmt.Println("wait client to connect....")
		conn, err := listen.Accept()
		if err != nil {
			fmt.Println("listen.Accept err=", err)
		}

		//connect success, create a groutine to keep the communication
		go process(conn)
	}
}